package org.nowcoder.leetcode.twoPointers;

/**
 * Title  : 11. Container With Most Water
 * Source : https://leetcode.cn/problems/container-with-most-water/?envType=study-plan-v2&envId=top-interview-150
 * Author : XrazYang
 * Date   : 2023-07-10
 */

public class LeetCode_11 {
    public int maxArea(int[] height) {
        //暴力求解： 打擂台，超时
        // int max = Integer.MIN_VALUE;
        // for (int i = 0; i < height.length - 1; i++) {
        //     for (int j = i + 1; j < height.length; j++) {
        //         int s = Math.min(height[i], height[j]) * (j - i);
        //         if (max < s) {
        //             max = s;
        //         }
        //     }
        // }
        // return max;

        //优化解法：双指针，主要难点在于指针的移动
        int left = 0, right = height.length - 1;
        int max = Integer.MIN_VALUE;
        while (left < right) {
            int s = Math.min(height[left], height[right]) * (right - left);
            if (max < s) {
                max = s;
            }
            if (height[left] <= height[right]) {
                left++;
            } else {
                right--;
            }
        }
        return max;
    }
}
